﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;


namespace inventory.Controls
{
    public class gridSaleCodeSum : common.controls.baseDataGridView 
    {
        public gridSaleCodeSum()
        {
            myItemSource.DataSource = DataAccess.DataCache.myMasterDS.Item;
            this.AutoGenerateColumns = false;
            this.myAutoFitColumn = ColumnNames.itemName.ToString();
            this.LockEdit(true);
        }
        BindingSource myItemSource = new BindingSource();

        // qtyRemain = qtySaleReal - qtyOutReal
        public enum ColumnNames
        {
            code,itemCode, itemName, itemUnit, qtyPlacement, qtySale, qtySaleReturn, qtySaleReal, 
            qtyOut, qtyOutReturn, qtyOutReal, qtyRemain
        };
        public void Init(data.tmpDS.saleCodeSumDataTable tbl)
        {
            Init(tbl, null);
        }
        public void Init(data.tmpDS.saleCodeSumDataTable tbl, string filter)
        {
            // 
            // code
            // 
            common.controls.DataGridViewTextBoxColumnExt code = new common.controls.DataGridViewTextBoxColumnExt();
            code.Uppercase = true;
            code.DataPropertyName = tbl.saleCodeColumn.ColumnName;
            code.HeaderText = "Mã.số";
            code.Name = ColumnNames.code.ToString();
            code.Width = 120;
            // 
            // itemCode
            // 
            common.controls.DataGridViewTextBoxColumnExt itemCode = new common.controls.DataGridViewTextBoxColumnExt();
            itemCode.Uppercase = true;
            itemCode.DataPropertyName = tbl.itemCodeColumn.ColumnName;
            itemCode.HeaderText = "Mã.hàng";
            itemCode.Name = ColumnNames.itemCode.ToString();
            itemCode.Width = 80;

            // 
            // itemName
            // 
            DataGridViewComboBoxColumn itemName = new DataGridViewComboBoxColumn();
            itemName.DataSource = this.myItemSource;
            itemName.DataPropertyName = tbl.itemCodeColumn.ColumnName;
            itemName.DisplayMember = DataAccess.DataCache.myMasterDS.Item.nameColumn.ColumnName;
            itemName.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            itemName.HeaderText = "Diễn giải";
            itemName.Name = ColumnNames.itemName.ToString();
            itemName.ReadOnly = true;

            itemName.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            itemName.Width = 370;
            itemName.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            itemName.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;

            // 
            // unit
            // 
            DataGridViewComboBoxColumn itemUnit = new DataGridViewComboBoxColumn();
            itemUnit.DataSource = this.myItemSource;
            itemUnit.DataPropertyName = tbl.itemCodeColumn.ColumnName;
            itemUnit.DisplayMember = DataAccess.DataCache.myMasterDS.Item.unitColumn.ColumnName;
            itemUnit.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            itemUnit.HeaderText = "ĐVT";
            itemUnit.Name = ColumnNames.itemUnit.ToString();
            itemUnit.ReadOnly = true;
            itemUnit.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            itemUnit.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            itemUnit.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;
            itemUnit.Width = 60;
            // 
            // Qty
            // 
            DataGridViewCellStyle qtyCellType = new DataGridViewCellStyle();
            qtyCellType.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            qtyCellType.Format = "N" + common.SysLibs.GetPrecisionFromMask(CommonTypes.Settings.sysMaskQty).ToString(); 
            qtyCellType.NullValue = null;

            DataGridViewTextBoxColumn qtyPlacement = new DataGridViewTextBoxColumn();
            qtyPlacement.DataPropertyName = tbl.qtyPlacementColumn.ColumnName;
            qtyPlacement.HeaderText = "Đặt.hàng";
            qtyPlacement.Name = ColumnNames.qtyPlacement.ToString();
            qtyPlacement.DefaultCellStyle = qtyCellType;
            qtyPlacement.Width = 70;

            DataGridViewTextBoxColumn qtySale = new DataGridViewTextBoxColumn();
            qtySale.DataPropertyName = tbl.qtySaleColumn.ColumnName;
            qtySale.HeaderText = "Bán.hàng";
            qtySale.Name = ColumnNames.qtySale.ToString();
            qtySale.DefaultCellStyle = qtyCellType;
            qtySale.Width = 80;

            DataGridViewTextBoxColumn qtySaleReturn = new DataGridViewTextBoxColumn();
            qtySaleReturn.DataPropertyName = tbl.qtySaleReturnColumn.ColumnName;
            qtySaleReturn.HeaderText = "Trả.lại";
            qtySaleReturn.Name = ColumnNames.qtySaleReturn.ToString();
            qtySaleReturn.DefaultCellStyle = qtyCellType;
            qtySaleReturn.Width = 70;

            DataGridViewTextBoxColumn qtySaleReal = new DataGridViewTextBoxColumn();
            qtySaleReal.DataPropertyName = tbl.qtySaleRealColumn.ColumnName;
            qtySaleReal.HeaderText = "Thực.bán";
            qtySaleReal.Name = ColumnNames.qtySaleReal.ToString();
            qtySaleReal.DefaultCellStyle = qtyCellType;
            qtySaleReal.Width = 70;
            qtySaleReal.Visible = false;

            DataGridViewTextBoxColumn qtyOut = new DataGridViewTextBoxColumn();
            qtyOut.DataPropertyName = tbl.qtyOutColumn.ColumnName;
            qtyOut.HeaderText = "Xuất.bán";
            qtyOut.Name = ColumnNames.qtyOut.ToString();
            qtyOut.DefaultCellStyle = qtyCellType;
            qtyOut.Width = 70;

            DataGridViewTextBoxColumn qtyOutReturn = new DataGridViewTextBoxColumn();
            qtyOutReturn.DataPropertyName = tbl.qtyOutReturnColumn.ColumnName;
            qtyOutReturn.HeaderText = "Nhập.trả";
            qtyOutReturn.Name = ColumnNames.qtyOutReturn.ToString();
            qtyOutReturn.DefaultCellStyle = qtyCellType;
            qtyOutReturn.Width = 70;

            DataGridViewTextBoxColumn qtyOutReal = new DataGridViewTextBoxColumn();
            qtyOutReal.DataPropertyName = tbl.qtyOutRealColumn.ColumnName;
            qtyOutReal.HeaderText = "Thực.Xuất";
            qtyOutReal.Name = ColumnNames.qtyOutReal.ToString();
            qtyOutReal.DefaultCellStyle = qtyCellType;
            qtyOutReal.Width = 70;
            qtyOutReal.Visible = false;

            DataGridViewTextBoxColumn qtyRemain = new DataGridViewTextBoxColumn();
            qtyRemain.DataPropertyName = tbl.qtyRemainColumn.ColumnName;
            qtyRemain.HeaderText = "Còn.lại";
            qtyRemain.Name = ColumnNames.qtyRemain.ToString();
            qtyRemain.DefaultCellStyle = qtyCellType;
            qtyRemain.Width = 70;

            // 
            // myDetailGrid
            // 
            this.Columns.Clear();
            this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] 
                            {code, itemCode, itemName, itemUnit, qtyPlacement, qtySale,qtySaleReturn,qtySaleReal, 
                              qtyOut, qtyOutReturn, qtyOutReal, qtyRemain });

            DataView dataView = new DataView(tbl);
            dataView.RowFilter = (filter == null ? "" : filter);
            this.DataSource = dataView;
            UpdateItemList();
            AutoArrange();
        }
        public new int RowCount
        {
            get
            {
                if (this.DataSource == null) return 0;
                return ((DataView)this.DataSource).Count;
            }
        }
        public void UpdateItemList()
        {
            DataView dataView = (DataView)this.DataSource;
            for (int idx = 0; idx < dataView.Count; idx++)
            {
                DataAccess.DataCache.FindITEM(((data.tmpDS.saleCodeSumRow)(dataView[idx] as DataRowView).Row).itemCode);
            }
        }
    }
}
